package com.example.myapplication.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.adp.PhotoUpAdp;
import com.example.myapplication.database.SellItem;
import com.example.myapplication.javaBean.PhotoBean;
import com.example.myapplication.util.ActivityStack;
import com.example.myapplication.util.DbUtils;
import com.example.myapplication.util.SelectUtils;
import com.example.myapplication.util.ToastUtil;

import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class PubSellAty extends BaseActivity {
    private TextView tvTitle,photoNum,tvType;
    private EditText etTitle,etPhone,etPrice,etDes,etTag;
    private RecyclerView listVIew;
    private View btPub;
    private PhotoUpAdp adp;
    private ArrayList<PhotoBean> photos=new ArrayList<>();
    private ArrayList<String> types=new ArrayList<>();

    @Override
    protected void logicStart() {
        adp=new PhotoUpAdp(photos);
        listVIew.setLayoutManager(new GridLayoutManager(this,4));
        listVIew.setAdapter(adp);

        types.add("书籍");
        types.add("电子");
        types.add("衣物");
        types.add("日杂");

    }

    @Override
    protected void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        photoNum=findViewById(R.id.photoNum);
        tvType=findViewById(R.id.tvType);
        etTag=findViewById(R.id.tvTag);

        etTitle=findViewById(R.id.etTitle);
        etPhone=findViewById(R.id.etPhone);
        etPrice=findViewById(R.id.etPrice);
        etDes=findViewById(R.id.etDes);
        listVIew=findViewById(R.id.listView);
        btPub=findViewById(R.id.btPub);


        tvTitle.setText("卖东西");

    }

    @Override
    protected void setListener() {
        super.setListener();
        tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectUtils.INSTANCE.showSelectPicker(PubSellAty.this, tvType, types, new Function1<Integer, Unit>() {
                    @Override
                    public Unit invoke(Integer integer) {
                        return null;
                    }
                },false);
            }
        });
        btPub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etTitle.getText().toString().isEmpty()){
                    ToastUtil.toast("请填写标题");
                    return;
                }
                if (etPhone.getText().toString().isEmpty()){
                    ToastUtil.toast("请填写手机号");
                    return;
                }
                if (tvType.getText().toString().isEmpty()){
                    ToastUtil.toast("请选择类型");
                    return;
                }
                if (etPrice.getText().toString().isEmpty()){
                    ToastUtil.toast("请填写价格");
                    return;

                }
                if (etDes.getText().toString().isEmpty()){
                    ToastUtil.toast("请填写简介");
                    return;
                }
                if (photos.size()<2){
                    ToastUtil.toast("请至少上传一张图片");
                    return;
                }
                //TODO 加弹框
                SellItem sellItem = new SellItem();

                sellItem.setTitle(etTitle.getText().toString());
                sellItem.setDes(etDes.getText().toString());
                sellItem.setPhone(etPhone.getText().toString());
                sellItem.setPrice(etPrice.getText().toString());
                sellItem.setType(tvType.getText().toString());
                sellItem.setSellerId(DbUtils.getLoginUser().getId());
                sellItem.setTag(etTag.getText().toString());

                ArrayList<String> strings = new ArrayList<>();
                for (PhotoBean photoBean :photos){
                    if (photoBean.getType()==1){
                        strings.add(photoBean.getPath());
                    }
                }
                sellItem.setPhotos(strings);
                sellItem.save();
                ToastUtil.toast("发布成功");
                ActivityStack.INSTANCE.popOut(PubSellAty.this,true);
            }

        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_pub_sell;
    }
}
